package com.zjgeport.zjgdanger.Exception;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.soar.util.net.rmi.httpxml.HttpCallBack;
import com.soar.util.net.rmi.httpxml.HttpClient;
import com.soar.util.net.rmi.httpxml.HttpMethod;
import com.soar.util.net.rmi.httpxml.HttpResult;
import com.zjgeport.zjgdanger.CommonActivity;
import com.zjgeport.zjgdanger.LoginActivity;
import com.zjgeport.zjgdanger.R;
import com.zjgeport.zjgdanger.common.ParamCommon;
import com.zjgeport.zjgdanger.model.myApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IllegalDetailActivity extends CommonActivity implements RadioGroup.OnCheckedChangeListener {

    private RelativeLayout rl3,rl4;
    private Button saveBtn;
    private RadioGroup checkRadioGroup;
    private RadioButton btn1,btn2;
    private EditText editText;
    private String ischeck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_illegal_detail);
        initCommonListener();
        initView();
    }

    private void initView() {
        ischeck = "1";
        TextView tv = findTextViewById(R.id.title);
        rl3 = (RelativeLayout)findViewById(R.id.rl3);
        rl4 = (RelativeLayout)findViewById(R.id.rl4);
        checkRadioGroup = (RadioGroup)findViewById(R.id.check_radioGroup);
        checkRadioGroup.setOnCheckedChangeListener(this);
        editText = (EditText)findViewById(R.id.checkedit);
        btn1 = (RadioButton)findViewById(R.id.check_y_btn);
        btn2 = (RadioButton)findViewById(R.id.check_n_btn);
        saveBtn = (Button)findViewById(R.id.save);

        saveBtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                HttpMethod method = new HttpMethod();
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put("ID", getIntent().getStringExtra("ID"));
                map.put("IS_HANDLE", ischeck);
                map.put("HANDLE_INFO", editText.getText().toString());
                map.put("UPDATE_USER", myApplication.getUser().getRealname());
                map.put("UPDATE_USER_ID", myApplication.getUser().getUserid());
                map.put("TOKEN", myApplication.getUser().getToken());
                method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
                method.setClassPath("com.mobile.Execute");
                method.setMethodName("DealIllegalExpt");
                method.setKvdata(map);
                final List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
                HttpClient.postdata(new Button(IllegalDetailActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
                    @Override
                    public void run(HttpResult result) {

                        if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)) {
                            showToast("处理成功");
                            finish();

                        }
                        else {
                            Toast.makeText(getApplication(), result.getResultMessage(), 1).show();
                            if (result.getResultCode().equals("90"))
                            {
                                Intent intent = new Intent(IllegalDetailActivity.this,LoginActivity.class);
                                startActivity(intent);
                            }
                        }


                    }
                });
            }
        });
        if ((getIntent().getStringExtra("FLAG")).equals("1")) {

            saveBtn.setVisibility(View.GONE);
            btn1.setClickable(false);
            btn1.setChecked(true);
            btn2.setClickable(false);
            editText.setEnabled(false);

        }

        tv.setText("违法异常详情");
        getAccidentDetail();

    }

    /**
     * 获取事故详情
     */
    private void getAccidentDetail() {

        HttpMethod method = new HttpMethod();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("ID", getIntent().getStringExtra("ID"));
        map.put("TOKEN", myApplication.getUser().getToken());
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("getIllegalDetailInfo");
        method.setKvdata(map);
        final List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        HttpClient.postdata(new Button(IllegalDetailActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {

                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)) {
                    List<HashMap<String, String>> data1 = result.getDataSet();
                    if (data1 != null && data1.size() != 0) {
                        data.addAll(data1);
                    }

                    markRead();
                    // 刷新UI
                    Looper mainLooper = Looper.getMainLooper();
                    MyHandler handler = new MyHandler(mainLooper);
                    Message msg = new Message();
                    msg.what = 999;
                    msg.obj = data;
                    handler.sendMessage(msg);

                }
                else{
                    Toast.makeText(getApplication(), result.getResultMessage(), 1).show();
                    if (result.getResultCode().equals("90"))
                    {
                        Intent intent = new Intent(IllegalDetailActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }


            }
        });
    }

    /**
     * 标记已读
     */
    private void markRead() {

        HttpMethod method = new HttpMethod();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("ID", getIntent().getStringExtra("ID"));
        map.put("TOKEN", myApplication.getUser().getToken());
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("markAcciRead");
        method.setKvdata(map);
        final List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        HttpClient.postdata(new Button(IllegalDetailActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {

                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)) {


                }
                else{
                    Toast.makeText(getApplication(), result.getResultMessage(), 1).show();
                    if (result.getResultCode().equals("90"))
                    {
                        Intent intent = new Intent(IllegalDetailActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }


            }
        });

    }

    private class MyHandler extends Handler
    {
        public MyHandler(Looper looper)
        {
            super(looper);
        }

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 999:
                    List<HashMap<String, String>> dataList = (List<HashMap<String, String>>) msg.obj;
                    HashMap<String,String> map = (HashMap<String,String>)dataList.get(0);
                    TextView t1 = (TextView)findViewById(R.id.t1);
                    TextView t2 = (TextView)findViewById(R.id.t2);
                    TextView t3 = (TextView)findViewById(R.id.t3);
                    TextView t4 = (TextView)findViewById(R.id.t4);
                    TextView t5 = (TextView)findViewById(R.id.t5);
                    TextView t6 = (TextView)findViewById(R.id.t6);
                    TextView t7 = (TextView)findViewById(R.id.t7);
                    TextView t8 = (TextView)findViewById(R.id.t8);
                    TextView t9 = (TextView)findViewById(R.id.t9);
                    TextView t10 = (TextView)findViewById(R.id.t10);
                    TextView t11 = (TextView)findViewById(R.id.t11);
                    TextView t12 = (TextView)findViewById(R.id.t12);
                    TextView t13 = (TextView)findViewById(R.id.t13);
                    TextView t14 = (TextView)findViewById(R.id.t14);
                    TextView t15 = (TextView)findViewById(R.id.t15);

                    t1.setText(map.get("TANK_CODE").toString());
                    t2.setText(map.get("TANK_NAME").toString());
                    t3.setText(map.get("COMPANY_NAME").toString());
                    t4.setText(map.get("MAX_CONT").toString());
                    t5.setText(map.get("TANK_RADIUS").toString());
                    t6.setText(map.get("TANK_HEIGHT").toString());
                    t7.setText(map.get("CARGO_NAME").toString());
                    t8.setText(map.get("CARGO_NUMBER").toString());
                    t9.setText(map.get("TANK_TEMP").toString());
                    t10.setText(map.get("LIQUID").toString());
                    t11.setText(map.get("RECORD_DATE").toString());
                    t12.setText(map.get("WORK_STATE").toString());
                    t13.setText("超范围");
                    t14.setText(map.get("ALARM_INFO").toString());
                    t15.setText(map.get("ALARM_DATE").toString());
                    editText.setText(map.get("HANDLE_INFO").toString());


                    break;

                default:
                    break;
            }
        }
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {// Q: 参数 group 暂时还没搞清什么用途
        switch (checkedId) {
            case R.id.check_y_btn:
                ischeck = "1";
                break;
            case R.id.check_n_btn:
                ischeck ="0";
                break;
            default:
                ischeck = "0";
                break;
        }
    }
}
